package com.example.blogcommon.utils;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonUtil {
    public static String toJson (Object o) {
        if (o == null) return null;
        try {
            return JSON.toJSONString(o);
        } catch (Exception e) {
            log.error("toJson error, e {}",e);
            return null;
        }
    }

    public static <T> T parseJson (String s, Class<T> tClass) {
        if (s == null || s.isEmpty() || tClass == null) return null;
        T t = null;
        try {
            t = JSON.parseObject(s, tClass);
        } catch (Exception e) {
            log.error("parseJson error, e {}",e);
            return null;
        }
        return t;
    }
}
